/* Prepare inputs for demographics at the market level; also create market-category effects, at HRRs   */
********** Demographics at the market level
clear all
set more off
cd ""  //target directory


*** Construct `ahaid_hrr'
*!!! Need to re-define MARKET to be HRR !!!
clear
insheet using "./prepare/hinfo_hrr.csv",clear
rename (v1 v2 v3 v4 v5 v6) (ahaid year prechs adj_vnd maxhrr sysid)
keep ahaid year maxhrr
tempfile othermkt
save `othermkt'


clear
use "./prepare/allbasic.dta",clear
merge m:1 ahaid year using `othermkt'
keep if _merge==3
drop _merge
keep if vid==1
keep id year maxhrr
* check how many HRR a hospital ever located during the sample period: ONE
bys id maxhrr: gen nvals = _n == 1 
by id: replace nvals = sum(nvals)
by id: replace nvals = nvals[_N] 
tab nvals
keep id year maxhrr
tempfile id_hrr
save `id_hrr'



*** Construct "hrr_fips"
** ZIP -- FIP (multiple zip to fips)
clear
insheet using "./rawdata/zip_fips.csv",clear
keep zip stcountyfp
rename stcountyfp fips
tempfile zipfip
save `zipfip'


** ZIP -- HRR (unique zip to hrr); then merge with `zipfip' to get `hrrfips' (multiple fips to hrr)
clear
foreach i of num 5/9 {
import excel using "./rawdata/ZipHsaHrr0`i'.xls",clear firstrow
keep hrrnum zipcode0`i'
rename (hrrnum zipcode0`i') (maxhrr zip)
merge 1:m zip using `zipfip'
keep if _merge==3
drop _merge
keep fips maxhrr
local yr=2000+`i'
gen year=`yr'
duplicates drop fips maxhrr,force
tempfile hrrfips`yr'
save `hrrfips`yr''
}


** Append all `hrrfips`yr''
clear
use `hrrfips2005'
append using `hrrfips2006'
append using `hrrfips2007'
append using `hrrfips2008'
append using `hrrfips2009'
tempfile hrrfips
save `hrrfips'



*** Construct demographics
clear
use "./rawdata/population demographics 1990_2012.dta", clear
keep fips year popest_tot popest_age65plus
drop if year<1997|year>2011
tempfile pop
save `pop'


clear
use "./rawdata/AHRF county controls 1975_2013.dta", clear
keep FIPS Year IncPC
rename (FIPS Year IncPC) (fips year pcinc)
drop if year<1997|year>2011
merge 1:1 fips year using `pop'
keep if _merge==3
drop _merge
gen incpop=pcinc*popest_tot
merge 1:m fips year using `hrrfips'
keep if _merge==3
drop _merge


bys maxhrr year:egen totpop=total(popest_tot)
bys maxhrr year:egen totpop65=total(popest_age65plus)

bys maxhrr year:egen totincpop=total(incpop)
gen tmp_popest_tot=popest_tot
replace tmp_popest_tot=. if pcinc==.
bys maxhrr year:egen tot_tmp_popest_tot=total(tmp_popest_tot)
gen avginc=totincpop/tot_tmp_popest_tot


duplicates drop maxhrr year,force
keep maxhrr year totpop totpop65 avginc


merge 1:m maxhrr year using `id_hrr'
keep if _merge==3
drop _merge


save "./prepare/demographic4hrr",replace





**********
**********
**********  Estimate the market fixed effects (RENAME maxhrr to hsa!!)
clear all
set more off
set matsize 10000
cd ""  //target directory


*** Construct `ahaid_hrr'
*!!! Need to re-define MARKET to be HRR !!!
clear
insheet using "./prepare/hinfo_hrr.csv",clear
rename (v1 v2 v3 v4 v5 v6) (ahaid year prechs adj_vnd maxhrr sysid)
keep ahaid year maxhrr
tempfile othermkt
save `othermkt'



*** Demographics: population and income per capita
clear
use "./prepare/demographic4hrr"
rename maxhrr hsa
duplicates drop hsa year,force
keep hsa year totpop totpop65 avginc
gen perc65=totpop65/totpop
replace totpop=log(totpop)
replace totpop65=log(totpop65)
replace avginc=log(avginc)
tempfile pop
save `pop'
***



clear
use "./prepare/allbasic.dta"
merge m:1 ahaid year using `othermkt'
keep if _merge==3
drop _merge
rename hsa hsa_ori
rename maxhrr hsa



*** Create HHI
preserve
keep id year admtot hsa
bys hsa year:egen totadm_mkt=total(admtot)
gen admsh=admtot/totadm_mkt
bys hsa year:egen hhi=total(admsh^2)
duplicates drop hsa year,force
keep hsa year hhi
tempfile hhi
save `hhi'
restore
***



merge m:1 hsa year using `hhi'
keep if _merge==3
drop _merge


merge m:1 hsa year using `pop'
keep if _merge==3
drop _merge




*** Calculate # vendors per market
gen const=1
bys hsa year:egen nhosp=total(const)
bys hsa year:egen totadp=total(adj_vnd>1)
gen adpratio=totadp/nhosp


** ### UPDATED on 11/22/2020: create adoption ratio, using bedsize as weights
bys hsa year:egen totadp_bed=total(bdtot*(adj_vnd>1))
bys hsa year:egen totbed=total(bdtot)
gen adpratio_bed=totadp_bed/totbed
**




*$* Combine all vendors together; use "adpratio" as DV (Original adpratio)
preserve
duplicates drop hsa year,force
xi:reg adpratio hhi totpop65 i.year i.hsa,nocon vce(cluster hsa) noomit
est sto comb
esttab comb using "./prepare/mktFEs_comb1.csv",replace nose not wide plain noobs nodep nonum
restore



